home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / X11 / httpd / support / htpasswd.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-09  |  3.9 KB  |  180 lines

  1. /*
  2.  * htpasswd.c: simple program for manipulating password file for NCSA httpd
  3.  * 
  4.  * Rob McCool
  5.  */
  6.  
  7. #include <sys/types.h>
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include <sys/signal.h>
  11.  
  12. #define LF 10
  13. #define CR 13
  14.  
  15. #define MAX_STRING_LEN 256
  16.  
  17. char *tn;
  18.  
  19. char *strd(char *s) {
  20.     char *d;
  21.  
  22.     d=(char *)malloc(strlen(s) + 1);
  23.     strcpy(d,s);
  24.     return(d);
  25. }
  26.  
  27. void getword(char *word, char *line, char stop) {
  28.     int x = 0,y;
  29.  
  30.     for(x=0;((line[x]) && (line[x] != stop));x++)
  31.         word[x] = line[x];
  32.  
  33.     word[x] = '\0';
  34.     if(line[x]) ++x;
  35.     y=0;
  36.  
  37.     while(line[y++] = line[x++]);
  38. }
  39.  
  40. int getline(char *s, int n, FILE *f) {
  41.     register int i=0;
  42.  
  43.     while(1) {
  44.         s[i] = (char)fgetc(f);
  45.  
  46.         if(s[i] == CR)
  47.             s[i] = fgetc(f);
  48.  
  49.         if((s[i] == 0x4) || (s[i] == LF) || (i == (n-1))) {
  50.             s[i] = '\0';
  51.             return (feof(f) ? 1 : 0);
  52.         }
  53.         ++i;
  54.     }
  55. }
  56.  
  57. void putline(FILE *f,char *l) {
  58.     int x;
  59.  
  60.     for(x=0;l[x];x++) fputc(l[x],f);
  61.     fputc('\n',f);
  62. }
  63.  
  64.  
  65. /* From local_passwd.c (C) Regents of Univ. of California blah blah */
  66. static unsigned char itoa64[] =         /* 0 ... 63 => ascii - 64 */
  67.         "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  68.  
  69. to64(s, v, n)
  70.   register char *s;
  71.   register long v;
  72.   register int n;
  73. {
  74.     while (--n >= 0) {
  75.         *s++ = itoa64[v&0x3f];
  76.         v >>= 6;
  77.     }
  78. }
  79.  
  80. char *getpass(char *prompt); /* ugh */
  81. char *crypt(char *pw, char *salt); /* why aren't these prototyped in include */
  82.  
  83. void add_password(char *user, FILE *f) {
  84.     char *pw, *cpw, salt[3];
  85.  
  86.     pw = strd(getpass("New password:"));
  87.     if(strcmp(pw,getpass("Re-type new password:"))) {
  88.         fprintf(stderr,"They don't match, sorry.\n");
  89.         if(tn)
  90.             unlink(tn);
  91.         exit(1);
  92.     }
  93.     (void)srand((int)time((time_t *)NULL));
  94.     to64(&salt[0],rand(),2);
  95.     cpw = crypt(pw,salt);
  96.     free(pw);
  97.     fprintf(f,"%s:%s\n",user,cpw);
  98. }
  99.  
  100. void usage() {
  101.     fprintf(stderr,"Usage: htpasswd [-c] passwordfile username\n");
  102.     fprintf(stderr,"The -c flag creates a new file.\n");
  103.     exit(1);
  104. }
  105.  
  106. void interrupted() {
  107.     fprintf(stderr,"Interrupted.\n");
  108.     if(tn) unlink(tn);
  109.     exit(1);
  110. }
  111.  
  112. main(int argc, char *argv[]) {
  113.     FILE *tfp,*f;
  114.     char user[MAX_STRING_LEN];
  115.     char line[MAX_STRING_LEN];
  116.     char l[MAX_STRING_LEN];
  117.     char w[MAX_STRING_LEN];
  118.     char command[MAX_STRING_LEN];
  119.     int found;
  120.  
  121.     tn = NULL;
  122.     signal(SIGINT,(void (*)())interrupted);
  123.     if(argc == 4) {
  124.         if(strcmp(argv[1],"-c"))
  125.             usage();
  126.         if(!(tfp = fopen(argv[2],"w"))) {
  127.             fprintf(stderr,"Could not open passwd file %s for writing.\n",
  128.                     argv[2]);
  129.             perror("fopen");
  130.             exit(1);
  131.         }
  132.         printf("Adding password for %s.\n",argv[3]);
  133.         add_password(argv[3],tfp);
  134.         fclose(tfp);
  135.         exit(0);
  136.     } else if(argc != 3) usage();
  137.  
  138.     tn = tmpnam(NULL);
  139.     if(!(tfp = fopen(tn,"w"))) {
  140.         fprintf(stderr,"Could not open temp file.\n");
  141.         exit(1);
  142.     }
  143.  
  144.     if(!(f = fopen(argv[1],"r"))) {
  145.         fprintf(stderr,
  146.                 "Could not open passwd file %s for reading.\n",argv[1]);
  147.         fprintf(stderr,"Use -c option to create new one.\n");
  148.         exit(1);
  149.     }
  150.     strcpy(user,argv[2]);
  151.  
  152.     found = 0;
  153.     while(!(getline(line,MAX_STRING_LEN,f))) {
  154.         if(found || (line[0] == '#') || (!line[0])) {
  155.             putline(tfp,line);
  156.             continue;
  157.         }
  158.         strcpy(l,line);
  159.         getword(w,l,':');
  160.         if(strcmp(user,w)) {
  161.             putline(tfp,line);
  162.             continue;
  163.         }
  164.         else {
  165.             printf("Changing password for user %s\n",user);
  166.             add_password(user,tfp);
  167.             found = 1;
  168.         }
  169.     }
  170.     if(!found) {
  171.         printf("Adding user %s\n",user);
  172.         add_password(user,tfp);
  173.     }
  174.     fclose(f);
  175.     fclose(tfp);
  176.     sprintf(command,"cp %s %s",tn,argv[1]);
  177.     system(command);
  178.     unlink(tn);
  179. }
  180.